const router = require('koa-router')()
const JWT = require('jsonwebtoken')
const userList = [{
  id: 1,
  username: 'zhangsan',
  password: 'zhangsan'
}, {
  id: 2,
  username: 'lisi',
  password: 'lisi'
}, {
  id: 3,
  username: 'wangwu',
  password: 'wangwu'
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body;
  const user = userList.find(v => v.username === username && v.password === password);
  if (user) {
    ctx.body = {
      code: 200,
      data: {
        userInfo: user,
        token: JWT.sign(user, '666666')
      },
      message: '登录成功'
    }
  } else {
    ctx.body = {
      code: 400,
      message: '账户密码错误'
    }
  }
})

module.exports = router
